package top.leyi.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import top.leyi.entity.user.SimpleUser;
import top.leyi.entity.user.User;
import top.leyi.entity.activity.Activity;
import top.leyi.entity.SQLUtil;
import top.leyi.entity.activity.DetailActivity;
import top.leyi.entity.activity.SimpleActivity;

import java.util.List;

public interface ActivityMapper {

    /**
     * @desc 粗略的查询活动
     * @param util
     * @return
     */
    @Select(" select id,name,pics  from activity a where is_delete =0 LIMIT #{limit} OFFSET #{offset} ")
    List<SimpleActivity> querySimpleActivity(SQLUtil util);



    @Select("select * from activity a where id  = #{value}  ")
    DetailActivity queryActivity(int id);

    @Select("select * from activity a where is_delete =0 LIMIT #{limit} OFFSET #{offset}  ")
    List<DetailActivity> queryDetilActivity(SQLUtil util);

    /**
     * 查询参加的人员
     * @param id
     * @return
     */

    @Select("SELECT u.id, u.headPic,u.username  FROM user_activity ua left join user u on u.id = ua.user_id where ua.activity_id = #{value}")
    List<SimpleUser> queryJoinPeople(int id);
    /**
     * @desc 添加一个活动
     * @param
     * @return
     */
    @Insert("INSERT INTO activity(name, descripe, place, pics) values(#{name},#{descripe},#{place},#{pics} )")
    void addActivity(Activity activity);
    /**
     * @desc 删除一个活动
     * @param
     * @return
     */

    @Delete("update activity set is_delete = 1 where id = #{id}")
    void deleteActivity(Activity activity);
    /**
     * @desc 修改一个活动
     * @param
     * @return
     */
    @Update("update activity set name = #{name},descripe = #{descripe},place = #{place}, pics = #{pics} " +
            "where id = #{id}")
    void updateActivity(Activity activity);

    /**
     * @desc 查询活动参加人数
     * @param activity
     * @return
     */
    @Select("SELECT COUNT(*) FROM user_activity where user_id = #{userId} and activity_id = #{id}")
    int queryIsJoin(DetailActivity activity);

    /**
     * @desc 参加活动
     * @param activity
     * @return
     */
    @Insert("INSERT INTO user_activity(user_id, activity_id) values(#{userId}, #{id})")
    void  joinActivity(DetailActivity activity);

    /**
     * 取消参加活动
     * @param activity
     */
    @Delete("DELETE FROM user_activity where user_id = #{userId} and activity_id = #{id}")
    void  cancelActivity(DetailActivity activity);




}
